<?php
class aftersales_data_refund {
	public function __construct($app) {
        $this->app = $app;
        $this->mdlApplyRefund = $this->app->model('apply_refund');
    }
    public function saveRefund($data){
    	$ret = $this->mdlApplyRefund->save($data);
    	if ($ret) {
    		$post = array(
                'method'=>'order.refund',
                'params'=>$data,
        	);
	        $erp_caller_obj = kernel::service('apioubao.rpc_erp_request');
	        $result = $erp_caller_obj->call_api_oubao($post);
	        $r = json_decode($result,true);
    	}
    }
    public function changeRefund($data){
        $this->mdlApplyRefund->update(array('status'=>$data['status'],'admin_content'=>$data['memo']),array('apply_id'=>$data['apply_id']));
        if ($data['status']==2) {
            //审核通过
            $time = time();
            $refunds = app::get('ectools')->model('refunds');
            $objPaymemtcfg = app::get('ectools')->model('payment_cfgs');
            $applyInfo = $this->mdlApplyRefund ->getRow('*',array('apply_id'=>$data['apply_id']));
            $obj_order = app::get('b2c')->model('orders');
            $subsdf = array('order_objects'=>array('*',array('order_items'=>array('*',array(':products'=>'*')))));
            $sdf_order = $obj_order->dump($applyInfo['order_id'],'*',$subsdf);
            if ($data['refund_type'] =='deposit') {
                $sdf['refund_type'] = 'deposit_refund';
            }
            $sdf['order_id'] = $sdf_order['order_id'];
            $sdf['pay_type'] = 'offline';
            $sdf['payment'] = $sdf_order['payinfo']['pay_app_id'];;
            $sdf['money'] = $applyInfo['apply_money'];
            $sdf['return_score'] = 0;
            $sdf['op_name'] = 'admin';
            $sdf['status'] = 'ready';
            $sdf['refund_id'] = $refunds->gen_id();
            $sdf['pay_app_id'] = $sdf['payment'];
            $sdf['member_id'] = $sdf_order['member_id'];
            $sdf['currency'] = $sdf_order['currency'];
            $sdf['paycost'] = 0;
            $sdf['cur_money'] = $sdf['money'];
            $sdf['t_begin'] = $time;
            $sdf['t_payed'] = $time;
            $sdf['t_confirm'] = $time;
            $sdf['pay_object'] = 'order';
            $arrPaymentInfo = $objPaymemtcfg->getPaymentInfo($sdf['payment']);
            $sdf['app_name'] = $arrPaymentInfo['app_name'];
            $sdf['app_version'] = $arrPaymentInfo['app_version'];
            $obj_refunds = kernel::single("ectools_refund");
            // return $obj_refunds->generate($sdf);
            if ($obj_refunds->generate($sdf)){//产生退款单
                $is_refund_finished = false;
                $obj_refund_lists = kernel::servicelist("order.refund_finish");
                foreach ($obj_refund_lists as $order_refund_service_object){
                    //生成预存款审核任务
                    $is_refund_finished = $order_refund_service_object->order_refund_finish($sdf, 'succ', 'Back',$msg);
                }
            }
        }
    }
}